package hut.ai.team.screen;

import hut.ai.team.actor.Image;
import hut.ai.team.core.DragDropStage;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class SettingScreen extends AbstractScreen {
	private MyStage stage;
	private SettingDialog dlgSetting;
	private Image iCheckbox;
	private Label lbLevelNumb;
	private TextureRegionDrawable checkbox, checkboxChecked;
	public static boolean sBoolMusic = true;
	public static int sLevel = 1;
	Preferences prefs;
	
	private SettingScreen() {
		stage = new MyStage();
		
		initialize();
		
		prefs = Gdx.app.getPreferences("preferences");
	}
	
	private void initialize() {
		TextureAtlas uiAtlas = game.manager.get("images/ui/ui.atlas");
		
		//Drawable
		checkbox = new TextureRegionDrawable(uiAtlas.findRegion("checkbox-normal"));
		checkboxChecked = new TextureRegionDrawable(uiAtlas.findRegion("checkbox-checked"));
		
		// Background Setting
		Image bgSetting = new Image(uiAtlas.findRegion("bg1"));
		bgSetting.setFillParent(true);
		stage.addActor(bgSetting);
		
		BitmapFont fntNumberSetting = new BitmapFont(Gdx.files.internal("font/fontLevelLb.fnt"), uiAtlas.findRegion("fontLevelLb"), false);
		fntNumberSetting.scale(0.2f);
		BitmapFont fntSetting = new BitmapFont(Gdx.files.internal("font/fontSetting.fnt"), uiAtlas.findRegion("fontSetting"), false);
		BitmapFont fntSettingScaled = new BitmapFont(Gdx.files.internal("font/fontSetting.fnt"), uiAtlas.findRegion("fontSetting"), false);
		fntSettingScaled.scale(-0.35f);
		
		LabelStyle styleSetting = new LabelStyle(fntSetting, Color.WHITE);
		LabelStyle styleSettingScaled = new LabelStyle(fntSettingScaled, Color.BLACK);
		LabelStyle styleNumberSetting = new LabelStyle(fntNumberSetting, Color.WHITE);
		
		Label lbMusic = new Label("Music", styleSetting);
		lbMusic.setPosition(game.convertWidth(30), game.convertHeight(665));
		stage.addActor(lbMusic);
		
		Label lbBgMusic = new Label("Play background music", styleSettingScaled);
		lbBgMusic.setPosition(game.convertWidth(30), game.convertHeight(640));
		stage.addActor(lbBgMusic);
		
		Label lbLevel = new Label("Level", styleSetting);
		lbLevel.setPosition(game.convertWidth(30), game.convertHeight(565));
		lbLevel.setWidth(600);
		stage.addActor(lbLevel);
		
		Label lbLevelCom = new Label("Level for computer", styleSettingScaled);
		lbLevelCom.setPosition(game.convertWidth(30), game.convertHeight(540));
		lbLevelCom.setWidth(600);
		stage.addActor(lbLevelCom);
		
		lbLevelNumb = new Label("1", styleNumberSetting);
		lbLevelNumb.setPosition(game.convertWidth(426), game.convertHeight(550));
		lbLevelNumb.setWidth(600);
		stage.addActor(lbLevelNumb);
		
		iCheckbox = new Image(checkboxChecked);
		iCheckbox.setPosition(game.convertWidth(413), game.convertHeight(650));
		stage.addActor(iCheckbox);

		iCheckbox.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				onMusicClick();
			}
		});
		
		lbLevel.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				// game.setScreen(MenuScreen.getInstance());
				dlgSetting.setVisible(true);
			}
		});
		
		lbLevelCom.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				// game.setScreen(MenuScreen.getInstance());
				dlgSetting.setVisible(true);
			}
		});
		
		dlgSetting = new SettingDialog();
		stage.addActor(dlgSetting);
		dlgSetting.setVisible(false);
	}
	
	@Override
	protected void onBackKeyPress() {
		if (MenuScreen.BACK == 1) {
			game.setScreen(MenuScreen.getInstance());
		} else if (MenuScreen.BACK == 2) {
			game.setScreen(MainScreen.getInstance());
		}

	}
	
	private void onMusicClick() {
		if (sBoolMusic) {
			iCheckbox.setDrawable(new TextureRegionDrawable(checkbox));
		} else {
			iCheckbox.setDrawable(new TextureRegionDrawable(checkboxChecked));
		}
		sBoolMusic = !sBoolMusic;
		
		// Preferences
		prefs.putBoolean("soundOn", sBoolMusic);
		prefs.flush();
		prefs.getBoolean("soundOn");
	}
	
	private void onLevelClick() {
		// Preferences
		prefs.putInteger("level", sLevel);
		prefs.flush();
	}

	@Override
	public void show() {
		super.show();
		sBoolMusic = prefs.getBoolean("soundOn");
		sLevel = prefs.getInteger("level");
		if (sBoolMusic) {
			iCheckbox.setDrawable(new TextureRegionDrawable(checkboxChecked));
		} else {
			iCheckbox.setDrawable(new TextureRegionDrawable(checkbox));
		}
		lbLevelNumb.setText(""+sLevel);
	}
	
	@Override
	public Stage getStage() {
		return stage;
	}
	
	private static SettingScreen instance = null;
	public static SettingScreen getInstance() {
		if (instance == null) {
			instance = new SettingScreen();
		}
		return instance;
	}
	class MyStage extends DragDropStage {
		public MyStage() {
			super(game.assumeWidth, game.assumeHeight, false);
		}
	}
	
	class SettingDialog extends Group {
		private LabelStyle mLabelStyle;
		private Image iCheckbox1, iCheckbox2, iCheckbox3, iCheckbox4, iCheckbox5;
		private Label mLabel1, mLabel2, mLabel3, mLabel4, mLabel5;
		private int mChooseBox = 1;
		String mStrCheckbox = "1";
		
		public SettingDialog() {
			setSize(stage.getWidth(), stage.getHeight());
			
			TextureAtlas uiAtlas = game.manager.get("images/ui/ui.atlas");
			{
				BitmapFont FontSetting = new BitmapFont(Gdx.files.internal("font/fontLevel.fnt"), uiAtlas.findRegion("fontLevel"), false);
				mLabelStyle = new LabelStyle(FontSetting, Color.BLACK);
			}
			Image shadow = new Image(uiAtlas.findRegion("shadow"));
			shadow.setFillParent(true);
			addActor(shadow);
			
			
			Image bgSetting = new Image(uiAtlas.findRegion("dialog"));
			bgSetting.setPosition((stage.getWidth() - bgSetting.getWidth()) / 2, (stage.getHeight() - bgSetting.getHeight()) / 2);
			addActor(bgSetting);
			
			Image btnOk = new Image(uiAtlas.findRegion("button-setting-ok"));
			btnOk.setPosition(game.convertWidth(124), game.convertHeight(145));
			
			Image btnCancel = new Image(uiAtlas.findRegion("button-setting-cancel"));
			btnCancel.setPosition(game.convertWidth(257), game.convertHeight(145));
			
			btnOk.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					// TODO Auto-generated method stub
					okSetting();
					setVisible(false);
					// update label setting, generate question type
				}
			});
			btnCancel.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					// TODO Auto-generated method stub
					setVisible(false);
					refresh();
				}
			});
			
			iCheckbox1 = new Image(checkbox);
			iCheckbox1.setPosition(game.convertWidth(160), game.convertHeight(500));
			iCheckbox1.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setDrawableCheckbox(checkbox);
					iCheckbox1.setDrawable(checkboxChecked);
					mChooseBox = 1;
				}
			});
			iCheckbox2 = new Image(checkbox);
			iCheckbox2.setPosition(game.convertWidth(160), game.convertHeight(450));
			iCheckbox2.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setDrawableCheckbox(checkbox);
					iCheckbox2.setDrawable(checkboxChecked);
					mChooseBox = 2;
				}
			});
			iCheckbox3 = new Image(checkbox);
			iCheckbox3.setPosition(game.convertWidth(160), game.convertHeight(400));
			iCheckbox3.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setDrawableCheckbox(checkbox);
					iCheckbox3.setDrawable(checkboxChecked);
					mChooseBox = 3;
				}
			});
			iCheckbox4 = new Image(checkbox);
			iCheckbox4.setPosition(game.convertWidth(160), game.convertHeight(350));
			iCheckbox4.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setDrawableCheckbox(checkbox);
					iCheckbox4.setDrawable(checkboxChecked);
					mChooseBox = 4;
				}
			});
			iCheckbox5 = new Image(checkbox);
			iCheckbox5.setPosition(game.convertWidth(160), game.convertHeight(300));
			iCheckbox5.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setDrawableCheckbox(checkbox);
					iCheckbox5.setDrawable(checkboxChecked);
					mChooseBox = 5;
				}
			});
			mLabel1 = new Label("Level 1", mLabelStyle);
			mLabel1.setPosition(game.convertWidth(250), game.convertHeight(500));
			mLabel1.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setDrawableCheckbox(checkbox);
					iCheckbox1.setDrawable(checkboxChecked);
					mChooseBox = 1;
				}
			});
			mLabel2 = new Label("Level 2", mLabelStyle);
			mLabel2.setPosition(game.convertWidth(250), game.convertHeight(450));
			mLabel2.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setDrawableCheckbox(checkbox);
					iCheckbox2.setDrawable(checkboxChecked);
					mChooseBox = 2;
				}
			});
			mLabel3 = new Label("Level 3", mLabelStyle);
			mLabel3.setPosition(game.convertWidth(250), game.convertHeight(400));
			mLabel3.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setDrawableCheckbox(checkbox);
					iCheckbox3.setDrawable(checkboxChecked);
					mChooseBox = 3;
				}
			});
			mLabel4 = new Label("Level 4", mLabelStyle);
			mLabel4.setPosition(game.convertWidth(250), game.convertHeight(350));
			mLabel4.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setDrawableCheckbox(checkbox);
					iCheckbox4.setDrawable(checkboxChecked);
					mChooseBox = 4;
				}
			});
			
			mLabel5 = new Label("Level 5", mLabelStyle);
			mLabel5.setPosition(game.convertWidth(250), game.convertHeight(300));
			mLabel5.addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setDrawableCheckbox(checkbox);
					iCheckbox5.setDrawable(checkboxChecked);
					mChooseBox = 5;
				}
			});
			
			addActor(iCheckbox1);
			addActor(iCheckbox2);
			addActor(iCheckbox3);
			addActor(iCheckbox4);
			addActor(iCheckbox5);
			addActor(mLabel1);
			addActor(mLabel2);
			addActor(mLabel3);
			addActor(mLabel4);
			addActor(mLabel5);
			
			addActor(btnOk);
			addActor(btnCancel);
		}
		
		private void okSetting() {
			switch (mChooseBox) {
			case 1:
				sLevel = 1;
				lbLevelNumb.setText("1");
				break;
			case 2:
				sLevel = 2;
				lbLevelNumb.setText("2");
				break;
			case 3:
				sLevel = 3;
				lbLevelNumb.setText("3");
				break;
			case 4:
				sLevel = 4;
				lbLevelNumb.setText("4");
				break;
			case 5:
				sLevel = 5;
				lbLevelNumb.setText("5");
				break;
			}
			onLevelClick();
		}
		
		private void refresh() {
			switch (sLevel) {
			case 1:
				setDrawableCheckbox(checkbox);
				iCheckbox1.setDrawable(checkboxChecked);
				mChooseBox = 1;
				break;
			case 2:
				setDrawableCheckbox(checkbox);
				iCheckbox2.setDrawable(checkboxChecked);
				mChooseBox = 2;
				break;
			case 3:
				setDrawableCheckbox(checkbox);
				iCheckbox3.setDrawable(checkboxChecked);
				mChooseBox = 3;
				break;
			case 4:
				setDrawableCheckbox(checkbox);
				iCheckbox4.setDrawable(checkboxChecked);
				mChooseBox = 4;
				break;
			case 5:
				setDrawableCheckbox(checkbox);
				iCheckbox5.setDrawable(checkboxChecked);
				mChooseBox = 5;
				break;
			}
		}
		
		private void setDrawableCheckbox(TextureRegionDrawable drawable) {
			iCheckbox1.setDrawable(drawable);
			iCheckbox2.setDrawable(drawable);
			iCheckbox3.setDrawable(drawable);
			iCheckbox4.setDrawable(drawable);
			iCheckbox5.setDrawable(drawable);
		}
	}
}
